export default class _Set {
	#items = {}
	has(element) {
		return element in this.#items
	}
	add(element) {
		if (!this.has(element)) {
			this.#items[element] = element
			return true
		}
		return false
	}
	delete(element) {
		if (!this.#items.has(element)) {
			delete this.#items[element]
			return true
		}
		return false
	}
	clear() {
		this.#items = {}
	}
	size() {
		return Object.keys(this.#items).length
	}
	values() {
		return Object.values(this.#items)
	}
	union(otherSet) {
		const unionSet = new _Set()
		this.values().forEach(value => unionSet.add(value))
		otherSet.values().forEach(value => unionSet.add(value))
		return unionSet
	}
	intersection(otherSet) {
		const intersectionSet = new _Set()
		const values = this.values()
		const otherValues = otherSet.values()
		// 比较哪个元素多
		let biggerSet = values,
			smallerSet = otherValues
		if (otherValues.length - biggerSet.length > 0) {
			biggerSet = otherValues
			smallerSet = values
		}
		smallerSet.forEach(value => {
			if (biggerSet.includes(value)) {
				intersectionSet.add(value)
			}
		})
		return intersectionSet
	}
	difference(otherSet) {
		const differenceSet = new Set()
		this.values().forEach(value => {
			if (!otherSet.has(value)) {
				differenceSet.add(value)
			}
		})
		return differenceSet
	}
	isSubsetOf(otherSet) {
		if (this.size() > otherSet.size()) {
			return false
		}
		let isSubset = true
		this.values().every(value => {
			if (!otherSet.has(value)) {
				isSubset = false
				return false
			}
		})
		return isSubset
	}
}
